package com.learn.xml.util;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.net.URL;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.Map;
import org.dom4j.Attribute;
import org.dom4j.Element;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class ConfXmlParse extends ReadXmlUtil {

    private final static Logger log = LoggerFactory.getLogger(ConfXmlParse.class);

    public ConfXmlParse() {
    }

    public static void main(String[] args) throws Exception {

        InputStream inputStream = ConfXmlParse.class.getResourceAsStream("/conf/configuration.xml");
        LinkedHashMap<String, Map> map = new ConfXmlParse().dictXmlParse(inputStream);
        for (Map.Entry<String, Map> entry:map.entrySet()){
            System.out.println(entry.getKey()+"="+entry.getValue());
            LinkedHashMap<String,Map> son= (LinkedHashMap<String, Map>) entry.getValue();

        }
    }


    public LinkedHashMap<String, Map> dictXmlParse(InputStream var1) throws Exception {
        new LinkedHashMap();
        LinkedHashMap var3 = new LinkedHashMap();

        try {
            Element var4 = this.readXml(var1);
            LinkedHashMap var2 = this.getXmlContent(var4.elementIterator());
            var3.put(var4.getName(), var2);
            return var3;
        } catch (Exception var5) {
            log.info(var5.getMessage());
            throw new Exception(var5.getMessage());
        }
    }

    @Override
    public LinkedHashMap<String, Object> parseXmlSpecial(Element var1) throws Exception {
        Attribute var2 = null;
        LinkedHashMap var3 = new LinkedHashMap();
        LinkedHashMap var4 = new LinkedHashMap();

        try {
            Iterator var5 = var1.attributeIterator();

            while(var5.hasNext()) {
                var2 = (Attribute)var5.next();
                if (!isEquals(var2.getName(), "item")) {
                    var3.put(var2.getName(), var2.getValue());
                }
            }

            if (var1.attribute("item") != null) {
                var4.put(var1.attribute("item").getValue(), var3);
                return var4;
            } else {
                return var3;
            }
        } catch (Exception var6) {
            log.error(var6.getMessage());
            throw new Exception(var6.getMessage());
        }
    }
    /**
     * 字符串 是否为空
     * @param str
     * @return true or false
     * @throws Exception
     */
    public static boolean isNotBank(String str) throws Exception {
        try {
            return str != null || str.length() > 0;
        } catch (Exception e) {
            log.error(e.getMessage());
            throw new Exception(e.getMessage());
        }
    }
    /**
     * 比较字符串内容 是否相同
     * @param strA
     * @param strB
     * @return
     * @throws Exception
     */
    public static boolean isEquals(String strA, String strB) throws Exception {
        if (isNotBank(strA) && isNotBank(strB)) {
            return strA.equals(strB);
        } else {
            log.info("比较字段存在为空的情况!!!");
            throw new Exception("比较字段存在为空的情况!!!");
        }
    }
}
